<?php
namespace app\common\model;
use think\Model;
class User extends Model{

    protected $uid;
    public function _initialize()
    {
        $this->uid = cookie('uid');
    }

    public function mobileIsExists($mobile){
        $res=$this->where(array('mobile'=>$mobile))->find();
        if($res){
            return true;
        }else{
            return false;
        }
    }
    //获取当前登陆用户信息
    public function getUinfo(){
        $res=$this->where(['id'=>cookie('uid')])->find();
        if($res){
            unset($res['pwd']);
            return $res;
        }else{
            return [];
        }
    }
    public function getList(){
        $res=$this->order('id desc')->paginate(2);
        return $res;
    }

    /**
     * 查看账户余额
     */
    public function hasmoney($price){
       $res = $this->find($this->uid);
       if(intval($res['balance'])<intval($price)){
            return false;
       }
    }


    //获取三级分销关系图
    public function userprofit($uid){
        $User = $this->find($uid);
        if($User['pid']==0){
            return false;
        }
        $allUser  = $this->select();
        $allUser1 = $allUser;
        $alluser2 = $allUser;
        foreach ($allUser as $k=>$v){
           if($User['pid']==$v['id']){
//               第一级
               $fuid1= $v['id'];
               $this->where('id',$uid)->setField('fuid1',$v['id']);
               foreach ($allUser1 as $kk=>$vv){
                   if($v['pid']==$vv['id']){
                       $this->where('id',$uid)->setField('fuid2',$vv['id']);
                       foreach($alluser2 as $kkk=>$vvv){
                           if($vv['pid']==$vvv['id']){
                               $this->where('id',$uid)->setField('fuid3',$vvv['id']);
                            }
                       }
                   }

               }
           }
        }
    }


}